package com.qujia.web.controller.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;


import com.qujia.common.core.domain.entity.SysDept;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedHashMap;

/*
 * <Title> CouponController </Title>
 * <Description> 优惠券 </Description>
 * @Author JK
 * @Date 2021年7月6日
 */
@RestController
@RequestMapping("/system/StationPay")
public class SysStationPay {


    @ResponseBody
    @PostMapping("/StationPay")
    public String getJhPayInfo(@RequestBody SysDept dept) throws Exception {
        String ss ="";
        JSONObject reqData = new JSONObject();
        reqData.put("mno",dept.getMno() );
        reqData.put("signType", "00");
        String req= SXFPaySign.getSXFPay(reqData);
        ///logger.debug("参数信息："+req);
        System.out.println("req:" + req);
        //此处不要改变reqData里面值的顺序用LinkedHashMap
        HashMap reqMap = JSON.parseObject(req, LinkedHashMap.class, Feature.OrderedField);
        //组装加密串
        String signContent = RSASignature.getOrderContent(reqMap);
        System.out.println("拼接后的参数：" + signContent);
        //logger.debug("拼接后的参数："+signContent);
        //sign
        String sign = RSASignature.encryptBASE64(RSASignature.sign(signContent, SXFPaySign.privateKey));
        System.out.println("============签名:" + sign);
        reqMap.put("sign", sign);
        String reqStr = JSON.toJSONString(reqMap);
        System.out.println("请求参数:" + reqMap);
        System.out.println("请求参数:" + reqStr);
        //调用第三方接口地址
        String url = "https://openapi.suixingpay.com/merchant/sign/getUrl";//生产地址
        String resultJson = HttpUtils.connectPostUrl(url, reqStr);
        System.out.println("返回信息：" + resultJson);
        //不要对reqData排序 所以用LinkedHashMap
        HashMap<String, Object> result = JSON.parseObject(resultJson, LinkedHashMap.class, Feature.OrderedField);
        if ("0000".equals(result.get("code"))) {
            //验签
            String signResult = result.get("sign").toString();
            result.remove("sign");
            String resultStr = RSASignature.getOrderContent(result);
            System.out.println("resultStr: " + resultStr);
            //sign
            String resultSign = RSASignature.encryptBASE64(RSASignature.sign(signContent, SXFPaySign.privateKey));
            System.out.println("resultSign: " + resultSign);
            //组装加密串
            if (RSASignature.doCheck(resultStr, signResult, SXFPaySign.sxfPublic)) {
                System.out.println("===================验签成功==============");
                //
                if(result.containsKey("reqData")){
                    //判断 bizCode bizCode =="0000" 则业务成功
                }

            }
        }

        return ss;
    }
}
